package com.yinkgh.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 给java model类加注释或者加注解
 * @author yinlong
 *
 */
public class AddAnnotationUtil {

	public static void main(String[] args) {
		
		String sourceAnnotationsString =
				"@ModelPropertyDesc(PropertyDesc=\"公司名称\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"行业\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"联系方式\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"注册地(省份)\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"注册地（市）\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"所属证监局\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"主营业务\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"企业性质\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"是否为金融企业\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"主体评级\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"主体初始评级\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"主体当前评级\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"负责人\")#"
						+"@ModelPropertyDesc(PropertyDesc=\"签字人\")#"

				;
//		String aaa = "a#b#c";
		String[] annotations = sourceAnnotationsString.split("#");
//		String[] annotations = aaa.split("#");
//		for (String string : annotations) {
//			System.out.println(string);
//		}
		getFile("D://workspace//workspace_tech_mylearn//BondSys//src//main//java//com//bondsys//model//BondCompany.java", 
		annotations);
		
		
	}
	
	/**
	 * 读取文件内容并把private属性前加指定的注释
	 * @param path
	 */
	public static void getFile(String path, String[] annotations)  {
		try {
			
			// 文件路径 
			FileReader reader = new FileReader(path);
//					FileReader reader = new FileReader("C://Users//zzjs44//Desktop//新建文件夹//iphone6记录文档//memoryLog.log");
			
			BufferedReader br = new BufferedReader(reader);
			String str = null;
			StringBuffer stringBuffer = new StringBuffer("");
			int annotationIndex = 0;
			// 按行读取文件
			while ((str = br.readLine()) != null) {
					//修改文件内容
					if (str.contains("private")) {
						//防止下标越界
						if (annotationIndex < annotations.length) {
							String anno = annotations[annotationIndex];
							stringBuffer.append("    "+anno + "\n"+str + "\n");
							annotationIndex++;
						} else {
							stringBuffer.append(str + "\n");
						}
					} else {
						stringBuffer.append(str + "\n");
					}
//					System.out.println(str);
					
			}
			System.out.println(stringBuffer.toString());
			br.close();
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
}
